<template>
  <el-dropdown @command="handleCommand" @visible-change="handleVisibleChange">
    <span class="avatar-dropdown">
      <el-avatar :src="avatar" class="user-avatar" />
      <div class="user-name">
        <span class="hidden-xs-only">{{ username }}</span>
        <vab-icon
          :class="{ 'vab-dropdown-active': active }"
          class="vab-dropdown"
          icon="arrow-down-s-line"
        />
      </div>
    </span>
    <template #dropdown>
      <el-dropdown-menu>
        <!-- <el-dropdown-item command="personalCenter">
          <vab-icon icon="user-line" />
          {{ translateTitle('个人中心') }}
        </el-dropdown-item> -->
        <el-dropdown-item command="logout">
          <vab-icon icon="logout-circle-r-line" />
          {{ translateTitle('退出登录') }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script>
  import { translateTitle } from '@/utils/i18n'
  import { mapActions, mapGetters } from 'vuex'
  import { toLoginRoute } from '@/utils/routes'
  import { getLoginUser } from '@/api/common'

  export default {
    name: 'VabAvatar',
    data() {
      return {
        active: false,
        avatar: '',
        username: '',
      }
    },
    computed: {
      ...mapGetters({
        //avatar: 'user/avatar',
        //username: 'user/username',
      }),
    },
    created() {
      this.getuser()
      if(!!sessionStorage.getItem('userhead')){
        this.avatar = sessionStorage.getItem('userhead')
      }
      if(!!sessionStorage.getItem('username')){
        this.username = sessionStorage.getItem('username')
      }
    },
    methods: {
      translateTitle,
      ...mapActions({
        _logout: 'user/logout',
      }),
      getuser() {
        getLoginUser().then((data) => {
          if(data.code === 2000){
            console.log(111,data)
            this.avatar = data.data.logo
            sessionStorage.setItem('userhead', data.data.logo)
            this.username = data.data.nickname
            sessionStorage.setItem('username', data.data.nickname)
          }else{
            this.logout()
          }
        })
      },
      handleCommand(command) {
        switch (command) {
          case 'logout':
            this.logout()
            break
          case 'personalCenter':
            this.personalCenter()
            break
        }
      },
      handleVisibleChange(val) {
        this.active = val
      },
      personalCenter() {
        this.$router.push('/setting/personalCenter')
      },
      async logout() {
        await this._logout()
        await this.$router.push(toLoginRoute(this.$route.path))
      },
    },
  }
</script>

<style lang="scss" scoped>
  .avatar-dropdown {
    display: flex;
    align-content: center;
    align-items: center;
    justify-content: center;
    justify-items: center;

    .user-avatar {
      width: 40px;
      height: 40px;
      margin-left: 15px;
      cursor: pointer;
      border-radius: 50%;
    }

    .user-name {
      position: relative;
      display: flex;
      align-content: center;
      align-items: center;
      height: 40px;
      margin-left: 6px;
      line-height: 40px;
      cursor: pointer;

      [class*='ri-'] {
        margin-left: 0 !important;
      }
    }
  }
</style>
